import java.util.HashMap;
import java.util.Map;


/**
 * @author LKQ
 * @date 2021/12/31 12:05
 * @description sb方法，执行时间干到2500ms
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.checkInclusion("hello", "ooolleoooleh"));
    }

    public boolean checkInclusion(String s1, String s2) {
        if (s2.length() < s1.length()) {
            return false;
        }
        Map<Character, Integer> map = new HashMap<>();
        for (char c: s1.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        int n = s1.length();
        for (int i = 0; i < s2.length(); i++) {
            Map<Character, Integer> temp = new HashMap<>(map);
            int count = 0;
            int start = i;
            while (count < n && i < s2.length() && temp.getOrDefault(s2.charAt(i), 0) > 0) {
                temp.put(s2.charAt(i), temp.get(s2.charAt(i)) - 1);
                count++;
                i++;
            }
            i = start;
            if (count == n) {
                return true;
            }
        }
        return false;
    }
}
